package main

import (
	"flag"
	"fmt"
	"log"
	"os"

	"github.com/user/go-capcut/pkg/client"
)

func main() {
	// 定义命令行参数
	baseURL := flag.String("url", "http://localhost:8080", "API服务器地址")
	command := flag.String("cmd", "help", "要执行的命令: create, add-video, add-audio, add-text, add-image, add-effect, save, help")
	draftID := flag.String("draft", "", "草稿ID")
	videoURL := flag.String("video", "", "视频URL")
	audioURL := flag.String("audio", "", "音频URL")
	imageURL := flag.String("image", "", "图片URL")
	text := flag.String("text", "", "文本内容")
	color := flag.String("color", "#FFFFFF", "文本颜色")
	fontSize := flag.Int("font-size", 36, "字体大小")
	duration := flag.Float64("duration", 5.0, "持续时间")
	trackIndex := flag.Int("track", -1, "轨道索引")
	segmentID := flag.String("segment", "", "片段ID")
	effectType := flag.String("effect-type", "", "特效类型")
	effectName := flag.String("effect-name", "", "特效名称")
	startTime := flag.Float64("start", 0.0, "开始时间")
	endTime := flag.Float64("end", -1.0, "结束时间")
	volume := flag.Float64("volume", 100.0, "音量")
	width := flag.Int("width", 720, "视频宽度")
	height := flag.Int("height", 1280, "视频高度")

	// 解析命令行参数
	flag.Parse()

	// 创建客户端
	c := client.NewClient(*baseURL)

	// 执行命令
	switch *command {
	case "create":
		// 创建草稿
		draftID, draftURL, err := c.CreateDraft(*width, *height, *draftID)
		if err != nil {
			log.Fatalf("创建草稿失败: %v", err)
		}
		fmt.Printf("创建草稿成功，ID: %s, URL: %s\n", draftID, draftURL)

	case "add-video":
		// 添加视频
		if *draftID == "" || *videoURL == "" {
			log.Fatalf("缺少必要参数: draft, video")
		}
		trackIndex, segmentID, err := c.AddVideo(*draftID, *videoURL, *startTime, *endTime, *trackIndex, *volume)
		if err != nil {
			log.Fatalf("添加视频失败: %v", err)
		}
		fmt.Printf("添加视频成功，轨道索引: %d, 片段ID: %s\n", trackIndex, segmentID)

	case "add-audio":
		// 添加音频
		if *draftID == "" || *audioURL == "" {
			log.Fatalf("缺少必要参数: draft, audio")
		}
		trackIndex, segmentID, err := c.AddAudio(*draftID, *audioURL, *startTime, *endTime, *trackIndex, *volume)
		if err != nil {
			log.Fatalf("添加音频失败: %v", err)
		}
		fmt.Printf("添加音频成功，轨道索引: %d, 片段ID: %s\n", trackIndex, segmentID)

	case "add-text":
		// 添加文本
		if *draftID == "" || *text == "" {
			log.Fatalf("缺少必要参数: draft, text")
		}
		trackIndex, segmentID, err := c.AddText(*draftID, *text, *color, *fontSize, *duration, *trackIndex)
		if err != nil {
			log.Fatalf("添加文本失败: %v", err)
		}
		fmt.Printf("添加文本成功，轨道索引: %d, 片段ID: %s\n", trackIndex, segmentID)

	case "add-image":
		// 添加图片
		if *draftID == "" || *imageURL == "" {
			log.Fatalf("缺少必要参数: draft, image")
		}
		trackIndex, segmentID, err := c.AddImage(*draftID, *imageURL, *duration, *trackIndex)
		if err != nil {
			log.Fatalf("添加图片失败: %v", err)
		}
		fmt.Printf("添加图片成功，轨道索引: %d, 片段ID: %s\n", trackIndex, segmentID)

	case "add-effect":
		// 添加特效
		if *draftID == "" || *segmentID == "" || *effectType == "" || *effectName == "" {
			log.Fatalf("缺少必要参数: draft, segment, effect-type, effect-name")
		}
		effectID, err := c.AddEffect(*draftID, *trackIndex, *segmentID, *effectType, *effectName, *startTime, *endTime)
		if err != nil {
			log.Fatalf("添加特效失败: %v", err)
		}
		fmt.Printf("添加特效成功，特效ID: %s\n", effectID)

	case "save":
		// 保存草稿
		if *draftID == "" {
			log.Fatalf("缺少必要参数: draft")
		}
		draftURL, err := c.SaveDraft(*draftID)
		if err != nil {
			log.Fatalf("保存草稿失败: %v", err)
		}
		fmt.Printf("保存草稿成功，URL: %s\n", draftURL)

	case "help":
		// 显示帮助信息
		fmt.Println("CapCut CLI 使用说明:")
		fmt.Println("  创建草稿: capcut-cli -cmd create [-width 720] [-height 1280] [-draft draft_id]")
		fmt.Println("  添加视频: capcut-cli -cmd add-video -draft draft_id -video video_url [-start 0] [-end -1] [-track -1] [-volume 100]")
		fmt.Println("  添加音频: capcut-cli -cmd add-audio -draft draft_id -audio audio_url [-start 0] [-end -1] [-track -1] [-volume 100]")
		fmt.Println("  添加文本: capcut-cli -cmd add-text -draft draft_id -text text [-color #FFFFFF] [-font-size 36] [-duration 5] [-track -1]")
		fmt.Println("  添加图片: capcut-cli -cmd add-image -draft draft_id -image image_url [-duration 5] [-track -1]")
		fmt.Println("  添加特效: capcut-cli -cmd add-effect -draft draft_id -track track_index -segment segment_id -effect-type type -effect-name name [-start 0] [-end -1]")
		fmt.Println("  保存草稿: capcut-cli -cmd save -draft draft_id")
		fmt.Println("  显示帮助: capcut-cli -cmd help")

	default:
		fmt.Printf("未知命令: %s\n", *command)
		os.Exit(1)
	}
}
